package com.hgx.su.common.pure.util.coordinate;


import java.util.List;


public class MapUtils {

    public static final String POINT = "point";

    public static final String SCOPE = "scope";

    /**
     * 校验定点是否在 points这个范围集中   points不能为空
     *
     * @param points 指定区域的经纬度坐标组成的集合
     * @param point  指定点的信息
     * @return 定点是否在 points这个范围集中
     */
    public static boolean checkPointInArea(List<AreaPoint> points, AreaPoint point) {
        if (points == null || points.isEmpty() || point == null) {
            return false;
        }
        boolean result = false;
        int jump = 0;
        //记录当前点到上一个围栏点之间隔了多少个定点
        for (int i = 0, j = points.size() - 1, n = points.size(); i < n; j = i++) {
            if (POINT.equals(points.get(i).getPointType())) {
                if (point.getLongitude().equals(points.get(i).getLongitude()) && point.getLatitude().equals(points.get(i).getLatitude())) {
                    return true;
                }
                jump += 1;
                continue;
            }
            if (jump > j) {    //处理围栏点不在数组第一个位置的情况，此时jump值并不准确
                for (int k = n - 1; k < n; k--) {
                    //因为不判断J所在的点是否围栏点，因此需要倒推循环获取到数组内最后一个围栏点
                    if (SCOPE.equals(point.getPointType())) {
                        j = k;
                        break;
                    }
                }
            }
            //其他任意情况，j始终大于jump，且jump值是准确的
            else {
                j -= jump;
            }
            // 射线法：与多边形相交一次，result布尔值取反一次，奇数个则在区域内
            if ((points.get(i).getLongitude() > point.getLongitude()) != (points.get(j).getLongitude() > point.getLongitude())
                    && (point.getLatitude() < (points.get(j).getLatitude() - points.get(i).getLatitude()) * (point.getLongitude() - points.get(i).getLongitude()) / (points.get(j).getLongitude() - points.get(i).getLongitude())
                    + points.get(i).getLatitude())) {
                result = !result;
            }
            jump = 0;
        }
        return result;
    }

    /**
     * 成都新都
     * [[104.216942,31.074298],
     * [104.08236,30.975444],
     * [104.226555,30.776249],
     * [104.416069,30.867058],
     * [104.402336,31.079003]]
     * <p>
     * 104.325903,30.944891 广汉机场
     */
//    public static void main(String[] args) {
//
//
//        double[] lon = {104.216942, 104.08236, 104.226555, 104.416069, 104.402336};
//        double[] lat = {31.074298, 30.975444, 30.776249, 30.867058, 31.079003};
//        List<AreaPoint> list = new ArrayList<>();
//        AreaPoint point;
//        for (int i = 0; i < lat.length; i++) {
//            point = new AreaPoint();
//            point.setLongitude(lon[i]);
//            point.setLatitude(lat[i]);
//            list.add(point);
//        }
//
//        AreaPoint po = new AreaPoint();
//        po.setLongitude(104.325903);
//        po.setLatitude(30.944891);
//
//        System.out.println(checkPointInArea(list, po));
//    }

}
